package dispatch_server

import (
	"encoding/json"
	"fmt"
	common "jihe.common"
	"jihe.common/protos/dispatch_server/proto"
	"jihe.common/tool"
	"time"
)

func DoCmd(areq proto.DispatchCmd) (dispatchRet proto.DispatchRet, err error) {
	areqStr, _ := json.Marshal(areq)
	r, _, errs := tool.GetRequest().Timeout(time.Millisecond*time.Duration(areq.Timeout+1000)).Post(fmt.Sprintf("%s/cmd/do", common.Configer.Algorithm.Addr)).AppendHeader("Content-Type", "application/json; encoding=utf-8").SendString(string(areqStr)).EndStruct(&dispatchRet)
	if len(errs) != 0 {
		err = errs[0]
		return
	}
	if r.StatusCode != 200 {
		err = fmt.Errorf("dispatch_server err StatusCode not 200")
		return
	}
	if dispatchRet.Code != 200 {
		err = fmt.Errorf("dispatch_server err code not 200")
		return
	}
	return
}
